package com.zking.frame.lsx;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;

import com.zking.db.BaseDao;

// 增加面板
public class AddCustomer extends JDialog {
	private static final long serialVersionUID = 1L;
	private Font font;
	
	private JPanel pl_flow;
	private JLabel lb;// 头标前
	private JLabel lb_name;// 客户name
	private JLabel lb_sex;// 客户sex
	private JLabel lb_pym;
	private JLabel lb_city;
	private JLabel lb_address;
	private JLabel lb_discount;
	private JTextField tf_name;
	private JTextField tf_sex;
	private JTextField tf_pym;
	private JTextField tf_city;
	private JTextField tf_address;
	private JTextField tf_discount;
	
	private JLabel lb_vip;
	private JLabel lb_khh;
	private JLabel lb_dh;
	private JLabel lb_yb;
	private JLabel lb_cz;
	private JLabel lb_em;
	private JLabel lb_lxr;
	private JLabel lb_lxrsj;
	private JLabel lb_qcys;
	private JLabel lb_y1;
	private JLabel lb_qcyf;
	private JLabel lb_y2;
	
	private JTextField tf_vip;
	private JTextField tf_khh;
	private JTextField tf_dh;
	private JTextField tf_yb;
	private JTextField tf_cz;
	private JTextField tf_em;
	private JTextField tf_lxr;
	private JTextField tf_lxrsj;
	private JTextField tf_qcys;
	private JTextField tf_qcyf;
	
	private JPanel pl_bottom;// 下部面板
	private JButton bt_qd;// 确定
	private JButton bt_qx;// 取消
	private List<Object> params;// 数据库参数
	private String creatTime;// 创建时间
	private String accountNumber;// 创建账户
	
	public AddCustomer(JDialog frame,boolean modal,List<String> lists,int num){
		super(frame, modal);
		font = new Font("宋体", Font.BOLD, 18);
		
		Container c = getContentPane();
		Color color0 = new Color(74, 101, 132);
		c.setBackground(color0);
		c.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		
		// 中部面板
		pl_flow = new JPanel(new FlowLayout());// 流布局
		pl_flow.setPreferredSize(new Dimension(680, 450));
		Color color1 = new Color(222, 231, 239);
		pl_flow.setBackground(color1);
		gbc.gridx=0;
		gbc.gridy=0;
		c.add(pl_flow,gbc);
		
		JLabel label0 = new JLabel();
		label0.setPreferredSize(new Dimension(250, 40));
		pl_flow.add(label0);
		
		lb = new JLabel("客户资料填写");
		lb.setFont(font);
		lb.setPreferredSize(new Dimension(400, 40));
		pl_flow.add(lb);
		
		lb_name = new JLabel("名 称：");
		lb_name.setFont(font);
		lb_name.setPreferredSize(new Dimension(90, 40));
		pl_flow.add(lb_name);
		
		tf_name = new JTextField();
		tf_name.setFont(font);
		tf_name.setPreferredSize(new Dimension(150, 25));
		tf_name.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_name);
		
		JLabel lbb = new JLabel();// 填充板
		lbb.setPreferredSize(new Dimension(125, 40));
		pl_flow.add(lbb);
		
		lb_sex = new JLabel("性 别：");
		lb_sex.setFont(font);
		lb_sex.setPreferredSize(new Dimension(80, 40));
		pl_flow.add(lb_sex);
		
		tf_sex = new JTextField();
		tf_sex.setFont(font);
		tf_sex.setPreferredSize(new Dimension(150, 25));
		tf_sex.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_sex);
		
		
		lb_pym = new JLabel("拼音码：");
		lb_pym.setFont(font);
		lb_pym.setPreferredSize(new Dimension(90, 40));
		pl_flow.add(lb_pym);
		
		tf_pym = new JTextField();
		tf_pym.setFont(font);
		tf_pym.setPreferredSize(new Dimension(150, 25));
		tf_pym.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_pym);
		
		JLabel label1 = new JLabel();// 填充板
		label1.setPreferredSize(new Dimension(125, 40));
		pl_flow.add(label1);
		
		lb_city = new JLabel("城市：");
		lb_city.setFont(font);
		lb_city.setPreferredSize(new Dimension(80, 40));
		pl_flow.add(lb_city);
		
		tf_city = new JTextField();
		tf_city.setFont(font);
		tf_city.setPreferredSize(new Dimension(150, 25));
		tf_city.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_city);
		
		lb_address = new JLabel("地址：");
		lb_address.setFont(font);
		lb_address.setPreferredSize(new Dimension(90, 40));
		pl_flow.add(lb_address);
		
		tf_address = new JTextField();
		tf_address.setFont(font);
		tf_address.setPreferredSize(new Dimension(150, 25));
		tf_address.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_address);
		
		JLabel label01 = new JLabel();// 填充板
		label01.setPreferredSize(new Dimension(115, 40));
		pl_flow.add(label01);
		
		lb_discount = new JLabel("折 扣：");
		lb_discount.setFont(font);
		lb_discount.setPreferredSize(new Dimension(90, 40));
		pl_flow.add(lb_discount);
		
		tf_discount = new JTextField("0.85");
		tf_discount.setFont(font);
		tf_discount.setPreferredSize(new Dimension(150, 25));
		tf_discount.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		tf_discount.setEditable(false);// 不能修改
		pl_flow.add(tf_discount);
		
		JLabel label2 = new JLabel();// 填充板
		label2.setPreferredSize(new Dimension(615, 3));
		label2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		pl_flow.add(label2);

		// 由于每一行的添加极其相似，所以写成 setHang方法
		lb_vip=new JLabel("vip：");
		tf_vip=new JTextField();
		lb_khh=new JLabel("开户行：");
		tf_khh=new JTextField();
		setHang(lb_vip, tf_vip,  lb_khh, tf_khh);
		
		lb_dh=new JLabel("电话：");
		tf_dh=new JTextField();
		lb_yb=new JLabel("邮编：");
		tf_yb=new JTextField();
		setHang(lb_dh, tf_dh, lb_yb, tf_yb );
		
		lb_cz=new JLabel( "传真：");
		tf_cz=new JTextField();
		lb_em=new JLabel("E-mail：");
		tf_em=new JTextField();
		setHang(lb_cz, tf_cz, lb_em, tf_em);
		
		lb_lxr=new JLabel("联系人：");
		tf_lxr=new JTextField();
		lb_lxrsj=new JLabel("手机：");
		tf_lxrsj=new JTextField();
		setHang(lb_lxr, tf_lxr, lb_lxrsj, tf_lxrsj);
		
		lb_qcys = new JLabel("期初应收：");
		lb_qcys.setFont(font);
		lb_qcys.setPreferredSize(new Dimension(110, 40));
		pl_flow.add(lb_qcys);
		
		tf_qcys = new JTextField("0.0");
		tf_qcys.setFont(font);
		tf_qcys.setPreferredSize(new Dimension(150, 25));
		tf_qcys.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_qcys);
		
		lb_y1 = new JLabel("元");
		lb_y1.setFont(font);
		pl_flow.add(lb_y1);
		
		JLabel label3 = new JLabel();// 填充板
		label3.setPreferredSize(new Dimension(60, 40));
		pl_flow.add(label3);
		
		lb_qcyf = new JLabel("期初应付：");
		lb_qcyf.setFont(font);
		lb_qcyf.setPreferredSize(new Dimension(120, 40));
		pl_flow.add(lb_qcyf);
		
		tf_qcyf = new JTextField("0.0");
		tf_qcyf.setFont(font);
		tf_qcyf.setPreferredSize(new Dimension(150, 25));
		tf_qcyf.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf_qcyf);
		
		lb_y2 = new JLabel("元");
		lb_y2.setFont(font);
		pl_flow.add(lb_y2);
		
		// 下部面板
		pl_bottom = new JPanel(new GridBagLayout());
		pl_bottom.setOpaque(false);// 透明
		pl_bottom.setPreferredSize(new Dimension(700, 90));
		gbc.gridx=0;
		gbc.gridy=1;
		c.add(pl_bottom,gbc);
		
		bt_qd = new JButton("确定");
		bt_qd.setFont(new Font("宋体", Font.BOLD, 14));
		bt_qd.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		bt_qd.setOpaque(false);
		bt_qd.setPreferredSize(new Dimension(100, 35));
		pl_bottom.add(bt_qd);
		bt_qd.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				lists.removeAll(lists);// 数据清楚
				Tools t = new Tools();
				// 判断号码是否对
				boolean boo=t.isMobileNO(tf_dh.getText());
				boolean flag = false;
				try {
					Double.valueOf(tf_discount.getText().toString());
					flag=true;
				} catch (Exception e2) {
					flag=false;
				}
				// 期初应收拾数字
				boolean pay = false;
				if ("".equals(tf_qcys.getText())||tf_qcys.getText().length()==0) {
					pay = true;
				}else{
					try {
						Double.valueOf(tf_qcys.getText().toString());
						pay=true;
					} catch (Exception e2) {
						pay=false;
					}
				}
				// 期初应付是数字
				boolean pay2 = false;
				if ("".equals(tf_qcyf.getText())||tf_qcyf.getText().length()==0) {
					pay2 = true;
				}else{
					try {
						Double.valueOf(tf_qcyf.getText().toString());
						pay2=true;
					} catch (Exception e2) {
						pay2=false;
					}
				}
				
				if ("".equals(tf_name.getText())||tf_name.getText().length()==0) {
					JOptionPane.showMessageDialog(AddCustomer.this, "名称不能为空");
				}else if ("".equals(tf_dh.getText())||tf_dh.getText().length()==0) {
					JOptionPane.showMessageDialog(AddCustomer.this, "电话不能为空");
				}else if(boo==false){
					JOptionPane.showMessageDialog(AddCustomer.this, "电话号码有误");
				}else if(flag==false){
						JOptionPane.showMessageDialog(AddCustomer.this, "折扣不是数字");
				}else if (pay==false) {
					JOptionPane.showMessageDialog(AddCustomer.this, "期初应收只能是数字");
				}else if (pay2==false) {
					JOptionPane.showMessageDialog(AddCustomer.this, "期初应付只能是数字");
				}else{
					// 修改后的数据
					lists.add(tf_name.getText());
					lists.add(tf_pym.getText());
					lists.add(tf_city.getText());
					lists.add(tf_address.getText());
					// 创建账号
					accountNumber=t.getCount(num);
					lists.add(accountNumber);
					
					lists.add(tf_dh.getText());
					lists.add(tf_khh.getText());
					lists.add(tf_yb.getText());
					lists.add(tf_cz.getText());
					lists.add(tf_em.getText());
					
					lists.add(tf_lxrsj.getText());
					lists.add(tf_lxr.getText());
					lists.add(tf_qcys.getText());
					lists.add(tf_qcyf.getText());
					lists.add(tf_sex.getText());
					
					lists.add(tf_discount.getText());
					// 创建时间
					creatTime=t.getTime();
					lists.add(creatTime);
					lists.add(tf_vip.getText());
					
					
					
					
					// 要传的参数
					params = new ArrayList<Object>();
					for (int i = 0; i < lists.size(); i++) {
						String data=lists.get(i);
						if(data==null||"".equals(data)||data.length()==0){
							params.add(null);
						}else{
							params.add(data);
						}
					}
					// 数据库操作
					BaseDao base=new BaseDao();
			    	String sql="insert into client("
			    			+ "name,pinYinCode,city,address,accountNumber,"
			    			+ "tel,bankOfDeposit,postalCode,fax,e_mail,"
			    			+ "contactTel,contactPerson,qcys,qcyf,sex,"
			    			+ "discount,creactTime,isVIP) values("
			    			+ "?,?,?,?,?,"
			    			+ "?,?,?,?,?,"
			    			+ "?,?,?,?,?,"
			    			+ "?,?,?)";//指定列插入
			    	try {
			    		// 转化成数组
			    		base.update(sql,params.toArray());
					} catch (SQLException e1) {
						e1.printStackTrace();
					}finally{
						try {
							base.closeConnection();
						} catch (SQLException e1) {
							e1.printStackTrace();
						}
					}
					// 退出现在的面板
					dispose();// 退出
					// 添加完之后刷新主面板
				}
			}
		});
		
		JLabel label4 = new JLabel();// 填充板
		label4.setPreferredSize(new Dimension(60, 30));
		pl_bottom.add(label4);
		
		bt_qx = new JButton("取消");
		bt_qx.setFont(new Font("宋体", Font.BOLD, 14));
		bt_qx.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		bt_qx.setOpaque(false);
		bt_qx.setPreferredSize(new Dimension(100, 35));
		pl_bottom.add(bt_qx);
		bt_qx.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// 移除
				lists.removeAll(lists);
				// 退出
				dispose();
			}
		});
		
		
		setTitle("新增客户资料");
		setSize(800, 650);
		setResizable(false);// 不可增加大小
		setLocationRelativeTo(null);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setVisible(true);
	}
	
	public void setHang(JLabel lb1,JTextField tf1,JLabel lb2,JTextField tf2){
		lb1.setFont(font);
		lb1.setPreferredSize(new Dimension(90, 40));
		pl_flow.add(lb1);
		
		tf1.setFont(font);
		tf1.setPreferredSize(new Dimension(150, 25));
		tf1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf1);
		
		JLabel label = new JLabel();// 填充板
		label.setPreferredSize(new Dimension(115, 40));
		pl_flow.add(label);
		
		lb2.setFont(font);
		lb2.setPreferredSize(new Dimension(90, 40));
		pl_flow.add(lb2);

		tf2.setFont(font);
		tf2.setPreferredSize(new Dimension(150, 25));
		tf2.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		pl_flow.add(tf2);
	}
}
